import {createRouter, createWebHistory, } from 'vue-router'
import daxie from "../util/upper"

// 路由自动化注册
const routerList = []
// 第一层的都是特殊用途的组件，手动输入到router里面了,例如404等
// 第二层
const modulesFiles1 = import.meta.globEager('../views/*/index.vue')
const modulesname1  = Object.keys(modulesFiles1).filter(()=>true)
modulesname1.forEach((item)=>{
    const name = item.split("/")[2]
    const canshu = modulesFiles1[item].default.canshu || ""
    // 如果有参数
    if(canshu){
        let casnhulist = ""
        canshu.forEach((item1)=>{
            casnhulist = casnhulist + "/:" + item1
        })
        routerList.push({
            name:daxie(name),
            path:"/" + name + canshu,
            component:()=>import(`../views/${name}/index.vue`),
            children:[],
        })
    }else {
        // 如果没有参数
        routerList.push({
            name: daxie(name),
            path: "/" + name,
            component: () => import(`../views/${name}/index.vue`),
            children: [],
        })
    }
})
// 第三层
const modulesFiles2 = import.meta.globEager('../views/*/*/index.vue')
const modulesname2  = Object.keys(modulesFiles2).filter(()=>true)
modulesname2.forEach((item)=>{
    const namelist = item.split("/")
    const name = namelist[3]
    const parentname = namelist[2]
    const canshu = modulesFiles2[item].default.canshu || ""
    routerList.forEach((item1)=>{
        if(item1.name === daxie(parentname)){
            // 如果有参数
            if(canshu){
                let casnhulist = ""
                canshu.forEach((item2)=>{
                    casnhulist = casnhulist + "/:" + item2
                })
                item1.children.push({
                    name:daxie(name),
                    path:"" + name + casnhulist,
                    component:()=>import(`../views/${parentname}/${name}/index.vue`),
                    children:[],
                })
            }else{
                // 没有参数
                item1.children.push({
                    name:daxie(name),
                    path:"" + name,
                    component:()=>import(`../views/${parentname}/${name}/index.vue`),
                    children:[],
                })
            }
        }
    })
})
// 第四层
const modulesFiles3 = import.meta.globEager('../views/*/*/*/index.vue')
const modulesname3  = Object.keys(modulesFiles3).filter(()=>true)
modulesname3.forEach((item)=>{
    const namelist = item.split("/")
    const name = namelist[4]
    const parentname = namelist[3]
    const yeyename = namelist[2]
    const canshu = modulesFiles3[item].default.canshu || ""
    routerList.forEach((item1)=>{
        if(item1.name === daxie(yeyename)){
            item1.children.forEach((item2)=>{
                if(item2.name===daxie(parentname)){
                    if(canshu){
                        let casnhulist = ""
                        canshu.forEach((item4)=>{
                            casnhulist = casnhulist + "/:" + item4
                        })
                        item2.children.push({
                            name:daxie(name),
                            path: ''+name + casnhulist,
                            component:()=>import(`../views/${yeyename}/${parentname}/${name}/index.vue`),
                            children:[],
                        })
                    }else{
                        item2.children.push({
                            name: daxie(name),
                            path: '' + name,
                            component: () => import(`../views/${yeyename}/${parentname}/${name}/index.vue`),
                            children: [],
                        })
                    }
                }
            })
        }
    })
})
// console.log(routerList,"kankan")
const routes = [
    {
        path: "/",
        redirect: "login",
        // component: () => import("@/views/home.vue")
    },
    ...routerList,
    {
        path: '/404',
        name: '404',
        component: () => import('../views/404.vue')
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404'
    },
]

console.log(routes,"路由表")

const router = createRouter({
    history: createWebHistory(),
    routes,
})
// 在VUE中路由遇到Error: Avoided redundant navigation to current location:报错显示是路由重复
const originalPush = router.push
router.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}



export default router
